home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
vb6_sr_1
/
convert.frm
next >
Wrap
Text File
|
1999-02-25
|
7KB
|
212 lines
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 1560
ClientLeft = 60
ClientTop = 375
ClientWidth = 6840
LinkTopic = "Form1"
ScaleHeight = 1560
ScaleWidth = 6840
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command1
Caption = "Convert Numeric Dollar Amount to Text "
Height = 375
Left = 120
TabIndex = 2
Top = 1080
Width = 6615
End
Begin VB.TextBox Text2
Height = 495
Left = 1440
TabIndex = 1
Top = 480
Width = 5295
End
Begin VB.TextBox Text1
Height = 495
Left = 120
TabIndex = 0
Top = 480
Width = 1215
End
Begin VB.Label Label1
Caption = $"Convert.frx":0000
Height = 615
Left = 120
TabIndex = 3
Top = 0
Width = 6615
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'*****************************************************
'Project: Convert.VBP
'Author: Burt Abreu
'Date: 21 January 1999
'*****************************************************
'Comments: This code will convert a numeric dollar
'amount into text for use in such applications as
'printing a check.
'
'This project was built using a Tip and code sent to
'the VB4UandME site by PineryJim@aol.com. It is posted
'here after the webmaster at VB4UandMe sent me this
'and a few other code items when he shut down the site.
'-----------------------------------------------------
'Bug Fixes & Modifications
'-----------------------------------------------------
'Author Date Comments
'-----------------------------------------------------
'Burt Abreu 01/21/99
'
'*-Converted the original code sample into this project.
'*-Added commenting and renamed some controls and
' variables.
'*-Removed GoSub and moved ParseChunk routine to sub.
'*-Added validation to insure that a value was entered in
' the text box.
'*****************************************************
'You may use this code freely in your own projects
'with the following conditions:
'
'No warranty is provided or should be assumed as to
'suitability of this code for any particular purpose
'and the author and webmaster of this site have no
'liablity. All code is provided "AS-IS" -use at your
'own risk.
'
'If you wish to post the code to another site or make
'it a part of any product, freeware or otherwise, whose
'main feature is the code itself then you must first
'request permission by contacting Burt Abreu at the
'Visual Basic Explorer site http://www.vbexplorer.com
'*******************************************************
Option Explicit
'Set up two arrays to hold string values we
'will use to convert numbers to words
Dim BigOnes(9) As String
Dim SmallOnes(19) As String
'Declare variables
Dim Dollars As String
Dim Cents As String
Dim Words As String
Dim Chunk As String
Dim Digits As Integer
Dim LeftDigit As Integer
Dim RightDigit As Integer
Public Sub ParseChunk()
Digits = Mid(Chunk, 1, 1)
If Digits > 0 Then
Words = Words & " " & SmallOnes(Digits) & " Hundred"
End If
Digits = Mid(Chunk, 2, 2)
If Digits > 19 Then
LeftDigit = Mid(Chunk, 2, 1)
RightDigit = Mid(Chunk, 3, 1)
Words = Words & " " & BigOnes(LeftDigit)
If RightDigit > 0 Then
Words = Words & " " & SmallOnes(RightDigit)
End If
Else
If Digits > 0 Then
Words = Words & " " & SmallOnes(Digits)
End If
End If
End Sub
Private Sub Command1_Click()
'format the incoming number to guarantee six digits
'to the left of the decimal point and two to the right
'and then separate the dollars from the cents
Text1.Text = Format(Text1.Text, "000000.00")
Dollars = Left(Text1.Text, 6)
Cents = Right(Text1.Text, 2)
Words = ""
'------------------------------------------------------------
'check to make sure incoming number is not too large. Since
'we are reading in the first 6 characters in the statement
'above "Dollars = Left(Text1.Text, 6)" this isn't likely but
'this would be a good place to limit dollar amounts so I left
'it here. For instance, you might replace the hard coded
'999999 with a variable "MaxDollars" if you wanted to
'------------------------------------------------------------
If Dollars > 999999 Then
Text2.Text = "Dollar amount is too large"
ElseIf Dollars = "" Then
Text2.Text = "Please enter an amount"
Exit Sub
End If
'separate the dollars into chunks
If Dollars = 0 Then
Words = "Zero"
Else
'first do the thousands
Chunk = Left(Dollars, 3)
If Chunk > 0 Then
ParseChunk
Words = Words & " Thousand"
End If
'do the rest of the dollars
Chunk = Right(Dollars, 3)
If Chunk > 0 Then
ParseChunk
End If
End If
'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
Text2.Text = Words
Exit Sub
End Sub
Private Sub Form_Load()
'Populate the arrays
BigOnes(1) = "Ten"
BigOnes(2) = "Twenty"
BigOnes(3) = "Thirty"
BigOnes(4) = "Forty"
BigOnes(5) = "Fifty"
BigOnes(6) = "Sixty"
BigOnes(7) = "Seventy"
BigOnes(8) = "Eighty"
BigOnes(9) = "Ninety"
SmallOnes(1) = "One"
SmallOnes(2) = "Two"
SmallOnes(3) = "Three"
SmallOnes(4) = "Four"
SmallOnes(5) = "Five"
SmallOnes(6) = "Six"
SmallOnes(7) = "Seven"
SmallOnes(8) = "Eight"
SmallOnes(9) = "Nine"
SmallOnes(10) = "Ten"
SmallOnes(11) = "Eleven"
SmallOnes(12) = "Twelve"
SmallOnes(13) = "Thirteen"
SmallOnes(14) = "Fourteen"
SmallOnes(15) = "Fifteen"
SmallOnes(16) = "Sixteen"
SmallOnes(17) = "Seventeen"
SmallOnes(18) = "Eighteen"
SmallOnes(19) = "Nineteen"
End Sub